<script setup>
import { ref, onMounted, reactive } from 'vue'
import { getAllReview, reviewAll } from '@/api/admin/index.js'
import { getPaginationConfig } from '@/components/pagination.js'
import { message } from 'ant-design-vue'
let dataSource = ref([])
// 分页参数配置
const paginationConfig = ref(getPaginationConfig())
const authorState = reactive({
  params: {
    reviewType: 5,
    pageNum: 1,
    pageSize: 10
  },
  loading: false,
})
onMounted(() => {
  loadData(authorState.params)
})


// 同意
const handleAgree = ({ id, mainId, reviewType}) => {
  const params = {
    id,
    mainId,
    reviewResult: 2,
    reviewType
  }
  reviewAll(params)
    .then(() => {
      message.success('操作成功')
      loadData(authorState.params)
    })
    .catch(err => {
      message.error('操作失败')
    })
}
// 拒绝
const handleDisagree = ({ id, mainId, reviewType}) => {
  const params = {
    id,
    mainId,
    reviewResult: 1,
    reviewType
  }
  reviewAll(params)
    .then(() => {
      message.success('操作成功')
      loadData(authorState.params)
    })
    .catch(err => {
      message.error('操作失败')
    })
}
const columns = [
  {
    title: '作者昵称',
    dataIndex: 'createdUser'
  },
  {
    title: '作者邮箱',
    dataIndex: 'createdEmail',
    align: 'center'
  },
  {
    title: '内容',
    dataIndex: 'mainTitle',
    align: 'center'
  },
  {
    title: '申请时间',
    dataIndex: 'createdTime',
    align: 'center',
  },
  {
    title: '申请状态',
    dataIndex: 'reviewResult',
    slots: {
      customRender: 'reviewResult',
    }
  },
  {
    title: '操作',
    dataIndex: 'action',
    slots: {
      customRender: 'action',
    },
    align: 'center'
  }
]
// 获取审核作者列表数据
const loadData = (params) => {
  authorState.loading = true
  getAllReview(params)
  .then(res => {
    dataSource.value = res.records || []
    paginationConfig.value.total = res.total
    authorState.loading = false
  })
  .finally(() => {
    authorState.loading = false
  })
}
// 监听Table
const handleTableChange = ({ current }) => {
  authorState.params.pageNum = current
  paginationConfig.value.current = current
  loadData(authorState.params)
}
</script>
<template>
  <a-table 
    :dataSource="dataSource"  
    :columns="columns" 
    :loading="authorState.loading"
    :rowKey="record => record.id" 
    :pagination="paginationConfig"
    @change="handleTableChange"
  >
    <template #reviewResult="{ record }">
        <a-tag v-if="record.reviewResult === 0" color="warning">审核中</a-tag>
        <a-tag v-if="record.reviewResult === 1" color="error">失败</a-tag>
        <a-tag v-if="record.reviewResult === 2" color="success">成功</a-tag>
    </template>
    <template #action="{ record }">
      <a-button v-if="record.reviewResult !== 2"  type="link" @click="() => handleAgree(record)">同意</a-button>
      <a-button  v-if="record.reviewResult !== 2" type="link" danger @click="() => handleDisagree(record)">拒绝</a-button>
    </template>
  </a-table>
</template>
